<?php
namespace app\common\model\cost;

use think\Model;
use think\facade\Session;
use app\common\model\user\User;

class Costjournalinfo extends Model
{
     // 设置当前模型对应的完整数据表名称
        protected $table = 'costjournalinfo';
        protected $pk = 'JournalInfoId';
        
        public function typeinfo(){
            return $this->hasOne('app\common\model\cost\Costtype','TypeId','TypeId');
        }
   /**
    * 增加费用
    * @param [type] $date    [description]
    * @param [type] $type_id [description]
    * @param [type] $price   [支付为- 收入为+]
    */
    public static function add_cost($CostjournalId,$date,$type_id,$price){
        //按照货物id从新调整数顺序
        if(empty($price)){
            return moerr('价格为0无需记录');
        }
        $date = date('Y-m-d',strtotime($date));
        $sb = Costjournalinfo::where('CostJournalId',$CostjournalId)->where('TypeId',$type_id)->find();
        if(empty($sb)){
            $sb = new Costjournalinfo;
            $sb->CostJournalId = $CostjournalId;
            $sb->UpDate = $date;
            $sb->PayCost = 0;
            $sb->RevenueCost = 0;
            $sb->TypeId = $type_id;
            $sb->save();
        }
        //添加到表
        if($price>0){
            $sb->RevenueCost = $sb->RevenueCost + $price;
        }else{
            $sb->PayCost = $sb->PayCost - $price;
        }
        $sb->save();
        return sok('记录成功');
    }


    /**
     * 修正费用
     * @param  [type] $CostjournalId [description]
     * @param  [type] $date          [description]
     * @param  [type] $type_id       [description]
     * @param  [type] $price         [description]
     * @return [type]                [description]
     */
     public static function re_cost($CostjournalId,$date,$type_id,$price){
        //按照货物id从新调整数顺序
        if(empty($price)){
            return moerr('价格为0无需记录');
        }
        $date = date('Y-m-d',strtotime($date));
        $sb = Costjournalinfo::where('CostJournalId',$CostjournalId)->where('TypeId',$type_id)->find();
        if(empty($sb)){
            $sb = new Costjournalinfo;
            $sb->CostJournalId = $CostjournalId;
            $sb->UpDate = $date;
            $sb->PayCost = 0;
            $sb->RevenueCost = 0;
            $sb->TypeId = $type_id;
            $sb->save();
        }
        //当天直接减除
        if($price>0){
            //添加收入
            $sb->RevenueCost = $sb->RevenueCost - $price;
        }else{
            //添加支出
            $sb->PayCost = $sb->PayCost + $price;
        }
    
        $sb->save();
        return sok('记录成功');
    }

    
     /**
     * 获取订单信息
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public static function getinfo($date='',$enddate='',$type_id=''){
        if (empty($date)) {
            $date = date('Y-m-d',time());
        }else{
            $date = date('Y-m-d',strtotime($date));
        }
        if(empty($enddate)){
            $rdata = Costjournalinfo::where('Update',$date);
        }else{
            $enddate = date('Y-m-d',strtotime($enddate));
            $rdata = Costjournalinfo::where('Update','>',$date)->where('Update','<=',$enddate);
        }

        if(!empty($type_id)){
            $rdata = $rdata->where('TypeId',$type_id);
        }
        $rdata = $rdata->group('TypeId')->field('TypeId,Sum(PayCost) as PayCost,sum(RevenueCost) as RevenueCost')->with('typeinfo')->select();
        $rdata = $rdata->visible(['TypeId','PayCost','RevenueCost','typeinfo.TypeName']);
        return $rdata; 
    }

    /**
     * 获取列表
     * @param  [type] $data    [主数据]
     * @param  string $type    [查询类型/hawa/buss]
     * @param  [type] $is_page [是否分页显示]
     * @return [type]          [description]
     */
    public static function usergetlist($data,$type = 'hawa',$is_page = true){

        $rdata = CostjournalInfo::where('UpDate','<>','');
        $userinfo = User::getinfo();
        if($type=='summary'){
            $data['field'] = ['UpDate','TypeId','CostjournalId as cid','PayCost','RevenueCost'];
        }else{
            return moerr('参数错误');
        }

        $option = come_model_option($data,$rdata,$is_page);
        $rdata = $option['rdata'];
        $data = $option['data'];
  
        $rdata = $rdata->select();
        if(!empty($visible)){
            $visible = array_merge(['JournalInfoId'],$visible);
            $rdata =  $rdata->visible($visible);
        }

        $ndata['list'] = $rdata->toArray();
        if($is_page){
            $ndata['pageinfo'] = ['pageindex'=>$data['pageindex'],'pagemax'=>$data['count']];
        }
        return $ndata;
    }
}
